package com.vitas.coin.ui.dialog;

import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;

import androidx.annotation.CallSuper;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.vitas.coin.R;

import java.lang.reflect.Field;

public abstract class BaseDialogFragment extends DialogFragment {

    private OnDismissListener mListener;
    private boolean isOk;
    private Object value;
    protected Handler h = new Handler(Looper.getMainLooper());

    @Override
    @CallSuper
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.MyDialogTheme);
    }

    @Override
    public LayoutInflater onGetLayoutInflater(@Nullable Bundle savedInstanceState) {
        LayoutInflater inflater = super.onGetLayoutInflater(savedInstanceState);
        if (getDialog() != null) {
            getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }
        return inflater;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity(), R.style.MyDialogTheme) {
            @Override
            public void onBackPressed() {
                dismiss();
            }
        };
        return dialog;
    }

    @Override
    @CallSuper
    public void show(FragmentManager manager, String tag) {
        try {
            super.show(manager, tag);
        } catch (IllegalStateException e) {
            FragmentTransaction ft = manager.beginTransaction();
            ft.add(this, tag);
            ft.commitAllowingStateLoss();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    @Override
    @CallSuper
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        if (mListener != null) {
            mListener.onDismiss(isOk, value);
        }
    }

    public <T extends BaseDialogFragment> T setOnDismissListener(OnDismissListener listener) {
        mListener = listener;
        return (T) this;
    }

    public interface OnDismissListener {
        void onDismiss(boolean isOk, Object value);
    }

    public void dismiss(boolean isOk, Object value) {
        this.isOk = isOk;
        this.value = value;
        dismiss();
    }

    @Override
    public void dismiss() {
        try {
            Field f = DialogFragment.class.getDeclaredField("mDismissed");
            f.setAccessible(true);
            f.set(this, false);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        try {
            dismissAllowingStateLoss();
        } catch (Throwable e) {
            e.printStackTrace();
            if (mListener != null) {
                mListener.onDismiss(isOk, value);
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        h.removeCallbacksAndMessages(null);
    }

    @Override
    public void setCancelable(boolean cancelable) {
        super.setCancelable(cancelable);
        h.removeCallbacks(cancel);
        if (!cancelable) {
            h.postDelayed(cancel, 5000);
        }
    }

    Runnable cancel = new Runnable() {
        @Override
        public void run() {
            setCancelable(true);
        }
    };
}
